﻿
using Common.Models;
using Core.Entities.Mater;
using Core.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Infrastructure.Services.Mater
{
    /// <summary>
    /// 物料信息服务
    /// </summary>
    public class SupplierService : BaseService<Supplier, string>
    {
        private readonly IDapperRepository _dapperRepository;

        public SupplierService(
            IBaseRepository<Supplier, string> repository,
            IPagedRepository<Supplier, string> pagedRepository,
            IDapperRepository dapperRepository)
            : base(repository, pagedRepository)
        {
            _dapperRepository = dapperRepository;
        }

        /// <summary>
        /// 根据物料编码查询
        /// </summary>
        /// <param name="materCode">物料编码</param>
        /// <returns>物料信息列表</returns>
        public async Task<IEnumerable<Supplier>> GetByMaterCodeAsync(string materCode)
        {
            var filter = new QueryFilter
            {
                Conditions = new List<FilterCondition>
                {
                    new FilterCondition { Field = "MaterCode", Operator = "=", Value = materCode }
                }
            };

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result.Data;
        }

        /// <summary>
        /// 根据物料名称模糊查询
        /// </summary>
        /// <param name="materName">物料名称</param>
        /// <returns>物料信息列表</returns>
        public async Task<IEnumerable<Supplier>> GetByMaterNameAsync(string materName)
        {
            var filter = new QueryFilter
            {
                Conditions = new List<FilterCondition>
                {
                    new FilterCondition { Field = "MaterName", Operator = "like", Value = materName }
                }
            };

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result.Data;
        }
        /// 根据物料名称模糊查询
        /// </summary>
        /// <param name="materName">物料名称</param>
        /// <returns>物料信息列表</returns>
        public async Task<PagedResult<Supplier>> GetBySiloInfoAsync(QueryFilter filter, Supplier Supplier)
        {
            // 初始化条件列表
            filter.Conditions = new List<FilterCondition>();

            // 添加物料名称模糊查询条件 (如果提供了物料名称)
            if (!string.IsNullOrEmpty(Supplier.SuName))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "SuName",
                    Operator = "like",
                    Value = Supplier.SuName
                });
            }

            // 添加物料名称模糊查询条件 (如果提供了物料名称)
            if (!string.IsNullOrEmpty(Supplier.SuCode))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "SuCode",
                    Operator = "like",
                    Value = Supplier.SuCode
                });
            }


            // 始终添加未删除的条件
            filter.Conditions.Add(new FilterCondition
            {
                Field = "IsDeleted",
                Operator = "=",
                Value = false
            });

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result;
        }
        #region 新增、修改、删除等业务


        #endregion
    }
}